//
//  MOAlbumDetailItemModel.m
//  WCImApp
//
//  Created by MOiMac on 2019/9/12.
//  Copyright © 2019 WCImApp. All rights reserved.
//

#import "MOAlbumDetailItemModel.h"
#import <Photos/Photos.h>
#import <TZImagePickerController/TZImagePickerHelper.h>
@implementation MOAlbumDetailItemModel

+ (NSDictionary *)modelCustomPropertyMapper {
    return @{
             @"itemId" : @"id",
             @"itemMD5" : @"md5",
             @"itemStatus" : @"status"
             };
}


//+ (MOAlbumDetailItemModel *)creatModelWithData:(id)modelData andItemType:(MOSecretAlbumItemType)itemType andAlbumID:(NSNumber *)albumId isFirst:(BOOL)isFirst {
//    
//    MOAlbumDetailItemModel *model = [MOAlbumDetailItemModel new];
//    model.type = itemType;
//    model.album_id = [albumId integerValue];
//    model.itemStatus = MOSecretAlbumItemStatus_Uploading;
//    model.upload_at = [NSString stringWithFormat:@"%ld",[MOModuleService mo_ICMessageHelperCurrentMessageTime]];
//    model.itemLocalId = [[NSString stringWithFormat:@"%.0f",[MOModuleService getNowServerTime]] integerValue];
//    if (isFirst) {
//        model.is_cover = @"1";
//    }
//    
//    if ([modelData isKindOfClass:[NSString class]]) {
//        NSString *strData = (NSString *)modelData;
//        model.src = strData;
//        AVURLAsset * asset = [AVURLAsset assetWithURL:[NSURL fileURLWithPath:strData]];
//        CMTime   time = [asset duration];
//        NSInteger seconds = ceil(time.value/time.timescale) * 1000;
//        model.video_len = seconds;
//        UIImage *thumbnailImage11 = [UIImage videoFramerateWithPath:strData];
//        model.width = thumbnailImage11.size.width;
//        model.height = thumbnailImage11.size.height;
//        NSData *imgdata11 = UIImageJPEGRepresentation(thumbnailImage11, 0.1);
//        NSDictionary *outputFileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:strData error:nil];
//        NSInteger vidSize = [outputFileAttributes fileSize];
//        NSInteger imgSize = imgdata11.length;
//        model.size = vidSize + imgSize;
//        model.localCover = imgdata11;
//
//        
//        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//                [MOAlbumDetailItemModel uploadVidToServerWithModel:model];
//            });
//        });
//
//        
//    } else if ([modelData isKindOfClass:[UIImage class]]) {
//        UIImage *img = (UIImage *)modelData;
//        NSData *imgdata11 = UIImageJPEGRepresentation(img, 0.5);
//        model.localCover = imgdata11;
//        model.width = img.size.width;
//        model.height = img.size.height;
//        model.size = imgdata11.length;
//        
//
//        NSString *imgMD5 = [QAStringTool getMD5DataWithData:model.localCover];
//
//        model.itemMD5 = imgMD5;
//
//        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//                [MOAlbumDetailItemModel uploadPicToServerWithModel:model];
//            });
//        });
//    }
//    [[MOModuleService getAlbumProgress] setObject:@"0" forKey:[NSString stringWithFormat:@"%ld",model.itemLocalId]];
//    [[MOAlbumDBManage defaultManager] insertAlbumItem:model];
//    return model;
//}

//+ (void)uploadPicToServerWithModel:(MOAlbumDetailItemModel *)model {
//    NSString *imgMD5 = [ChatStringUtil getMD5DataWithData:model.localCover];
//    NSString *strKey = [NSString stringWithFormat:@"%@/pic/%@.jpg",[[MOModuleService userId] md5].uppercaseString,imgMD5];
//
//    
//    dispatch_async([MOSecetAlbumNetUtil sharedMOSecetAlbumNetUtil].albumUploadQueue, ^{
//        NSLog(@"current1:%@",[NSThread currentThread]);
//
//        dispatch_semaphore_wait([MOSecetAlbumNetUtil sharedMOSecetAlbumNetUtil].albumUploadSemaphore, 15); //等待信号,当信号总量少于0 的时候就会一直等待 ,否则就可以正常的执行,并让信号总量-1
//        [MOModuleService qiniuUploadDataWithKey:strKey andLoaclData:model.localCover withItemId:[NSString stringWithFormat:@"%ld",model.itemLocalId] Block:^(NSInteger status, NSString * _Nonnull successLnk) {
//
//            if (status == 1) {
//                [[SDImageCache sharedImageCache] storeImageDataToDisk:model.localCover forKey:successLnk];
//                model.src = successLnk;
//                [[MOSecetAlbumNetUtil sharedMOSecetAlbumNetUtil] addAlbumItemWithAlbumID:@(model.album_id) itemModels:@[model] Block:^(QAResponseModel * _Nonnull response) {
//                    if (response.code == 200) {
//                        if ([[response.data allKeys] containsObject:@"Id"]) {
//                            model.itemId = [[response.data objectForKey:@"Id"] integerValue];
//                        } else {
//                            model.itemId = [[response.data objectForKey:@"id"] integerValue];
//                        }
//                        model.itemStatus = MOSecretAlbumItemStatus_Success;
//                        [[MOAlbumDBManage defaultManager] deleteAlbumItem:model];
//                    } else {
//                        model.itemStatus = MOSecretAlbumItemStatus_Fail;
//                        [[MOAlbumDBManage defaultManager] updateAlbumItem:model];
//                    }
//                    [[NSNotificationCenter defaultCenter] postNotificationName:@"uploadFinish" object:@{@"model":model}];
//
//                    dispatch_semaphore_signal([MOSecetAlbumNetUtil sharedMOSecetAlbumNetUtil].albumUploadSemaphore);
//                }];
//            } else {
//                model.src = strKey;
//
//                model.itemStatus = MOSecretAlbumItemStatus_Fail;
//                [[MOAlbumDBManage defaultManager] updateAlbumItem:model];
//                [[NSNotificationCenter defaultCenter] postNotificationName:@"uploadFinish" object:@{@"model":model}];
//
//                dispatch_semaphore_signal([MOSecetAlbumNetUtil sharedMOSecetAlbumNetUtil].albumUploadSemaphore);
//
//            }
//        }];
//    });
//}

//+ (void)uploadVidToServerWithModel:(MOAlbumDetailItemModel *)model {
//    NSString *filePath = model.src;
//    NSString *strThumKey = [NSString stringWithFormat:@"%@/vid/%@.jpg",[[MOModuleService userId] md5].uppercaseString,[ChatStringUtil getMD5DataWithData:model.localCover]];
//
//    
//    dispatch_async([MOSecetAlbumNetUtil sharedMOSecetAlbumNetUtil].albumUploadQueue, ^{
//        
//        dispatch_semaphore_wait([MOSecetAlbumNetUtil sharedMOSecetAlbumNetUtil].albumUploadSemaphore, 15); //等待信号,当信号总量少于0 的时候就会一直等待 ,否则就可以正常的执行,并让信号总量-1
//        [MOModuleService qiniuUploadDataWithKey:strThumKey andLoaclData:model.localCover withItemId:@"0" Block:^(NSInteger status, NSString * _Nonnull successLnk) {
//            NSString *thumLnk = successLnk;
//            if (status == 1) {
//                [[SDImageCache sharedImageCache] storeImageDataToDisk:model.localCover forKey:successLnk];
//
//                if (![ChatStringUtil isBlankString:[ChatStringUtil fileMD5String:filePath]]) {
//                    NSString *strKey = [NSString stringWithFormat:@"%@/vid/%@.mp4",[[MOModuleService userId] md5].uppercaseString,[ChatStringUtil fileMD5String:model.src]];
//                    [MOModuleService qiniuUploadWithKey:strKey andLoaclFilePath:filePath withItemId:[NSString stringWithFormat:@"%ld",model.itemLocalId] Block:^(NSInteger status, NSString * _Nonnull successLnk) {
//
//                        if (status == 1) {
//                            model.src = successLnk;
//                            model.itemMD5 = [ChatStringUtil fileMD5String:filePath];
//                            model.cover_src = thumLnk;
//                            [[MOSecetAlbumNetUtil sharedMOSecetAlbumNetUtil] addAlbumItemWithAlbumID:@(model.album_id) itemModels:@[model] Block:^(QAResponseModel * _Nonnull response) {
//                                if (response.code == 200) {
//                                    [MOModuleService mo_ICFileToolRemoveFileAtPath:filePath];
//                                    model.itemStatus = MOSecretAlbumItemStatus_Success;
//                                    if ([[response.data allKeys] containsObject:@"Id"]) {
//                                        model.itemId = [[response.data objectForKey:@"Id"] integerValue];
//                                    } else {
//                                        model.itemId = [[response.data objectForKey:@"id"] integerValue];
//                                    }
//                                    [[MOAlbumDBManage defaultManager] deleteAlbumItem:model];
//                                } else {
//                                    model.itemStatus = MOSecretAlbumItemStatus_Fail;
//                                    model.src = filePath;
//                                    [[MOAlbumDBManage defaultManager] updateAlbumItem:model];
//                                }
//                                [[NSNotificationCenter defaultCenter] postNotificationName:@"uploadFinish" object:@{@"model":model}];
//                                dispatch_semaphore_signal([MOSecetAlbumNetUtil sharedMOSecetAlbumNetUtil].albumUploadSemaphore);
//                            }];
//                        } else {
//                            model.itemStatus = MOSecretAlbumItemStatus_Fail;
//                            model.src = filePath;
//                            [[MOAlbumDBManage defaultManager] updateAlbumItem:model];
//                            [[NSNotificationCenter defaultCenter] postNotificationName:@"uploadFinish" object:@{@"model":model}];
//                            
//                            dispatch_semaphore_signal([MOSecetAlbumNetUtil sharedMOSecetAlbumNetUtil].albumUploadSemaphore);
//                        }
//                    }];
//                } else {
//                    model.itemStatus = MOSecretAlbumItemStatus_Fail;
//                    model.src = filePath;
//                    [[MOAlbumDBManage defaultManager] updateAlbumItem:model];
//                    [[NSNotificationCenter defaultCenter] postNotificationName:@"uploadFinish" object:@{@"model":model}];
//                    dispatch_semaphore_signal([MOSecetAlbumNetUtil sharedMOSecetAlbumNetUtil].albumUploadSemaphore);
//                }
//            } else {
//                model.itemStatus = MOSecretAlbumItemStatus_Fail;
//                model.src = filePath;
//                [[MOAlbumDBManage defaultManager] updateAlbumItem:model];
//                [[NSNotificationCenter defaultCenter] postNotificationName:@"uploadFinish" object:@{@"model":model}];
//
//                dispatch_semaphore_signal([MOSecetAlbumNetUtil sharedMOSecetAlbumNetUtil].albumUploadSemaphore);
//            }
//        }];
//    });
//}


// 视频时长
- (NSString *)timeLength {
    if (!_timeLength) {
        NSInteger totalTime = self.video_len / 1000;
        int seconds = totalTime % 60;
        int minutes = (totalTime / 60) % 60;
        int hours = (int)totalTime / 3600;
        if (hours > 0) {
            _timeLength = [NSString stringWithFormat:@"%d:%d:%02d",hours,minutes, seconds];
        }else {
            _timeLength =[NSString stringWithFormat:@"%d:%02d", minutes, seconds];
        }
    }
    return _timeLength;
}
@end
